Elementos de Álgebra Lineal
LES RECOMIENDO checar:
Libros/Cursos en la red:
ALGEBRA LINEAL CON JULIA Stephen Boyd & Lieven Vandenberghe
https://web.stanford.edu/~boyd/vmls/
3BLUE1BROWN EXCELENTE!
https://www.youtube.com/c/3blue1brown
GILBERT STRANG MIT
https://math.mit.edu/~gs/
Lenguajes de Programación:
Julia
https://github.com/fonsp/Pluto.jl
Anaconda Python
[https://docs.anaconda.com/anaconda/install/(https://docs.anaconda.com/anaconda/install/)
1)Sistemas de Ecuaciones lineales
2)Formulación matricial, Espacios vectoriales,columna,renglón,bases
3)Sistemas determinados, sobre,sub
4)Cuadrados mínimos
5)Problema eigenvectores,eigenvalores
5)Descomposición de valor singular
7)matrices,rotaciones,números complejos
using Symbolics
using LinearAlgebra,Latexify, Printf,WGLMakie#,Plots
WGLMakie.activate!()
@variables x y z
$$\begin{equation} \left[ \begin{array}{c} x \\ y \\ z \\ \end{array} \right] \end{equation}$$
round4 (generic function with 1 method)
Sistemas de ecuaciones lineales
$$\begin{equation} \left[ \begin{array}{c} x + 2 y \\ 2 x + 4 y \\ \end{array} \right] = \left[ \begin{array}{c} 0 \\ 0 \\ \end{array} \right] \end{equation}$$
latexify(A*[x;y;z] ~ [2;0;0])
$$\begin{equation} \left[ \begin{array}{c} x + 3 y + 5 z \\ 4 x + 5 y + 6 z \\ 7 x + 8 y + 11 z \\ \end{array} \right] = \left[ \begin{array}{c} 2 \\ 0 \\ 0 \\ \end{array} \right] \end{equation}$$
(f ~ [3;0;0])
$$\begin{equation} \left[ \begin{array}{c} x + 3 y + 5 z \\ 4 x + 5 y + 6 z \\ 7 x + 8 y + 11 z \\ \end{array} \right] = \left[ \begin{array}{c} 3 \\ 0 \\ 0 \\ \end{array} \right] \end{equation}$$
$$\begin{equation} \left[ \begin{array}{c} x \\ y \\ z \\ \end{array} \right] = \left[ \begin{array}{c} -1 \\ 0.2857 \\ 0.4286 \\ \end{array} \right] \end{equation}$$
El sistema de ecuaciones lo podemos escribir de esta forma
$$x * \vec{col_1A} + y * \vec{col_2A}+ z * \vec{col_3A} $$
begin
#using Latexify
struct Ket{T}
x::T
end
@latexrecipe function f(x::Ket)
return Expr(:latexifymerge, "", x.x, "")
end
latexify(:(x*$(Ket(A[:,1])) + y*$(Ket(A[:,2]))+z*$(Ket(A[:,3]))))
end
$$x \cdot \left[ \begin{array}{c} 1 \\ 4 \\ 7 \\ \end{array} \right] + y \cdot \left[ \begin{array}{c} 3 \\ 5 \\ 8 \\ \end{array} \right] + z \cdot \left[ \begin{array}{c} 5 \\ 6 \\ 11 \\ \end{array} \right]$$
El sistema de ecuaciones de 2x2
begin
#Symbolics.solve_for([f1[1] ~ 1.0,f1[2] ~ 0.0],[x y])
Bb=copy(B)
Bb=Bb+([0.5 0;0 0])
#f3=B*[x;y];
f3=Bb*[x;y]
#f3=(B+ones(2,2))*[x;y];
(f3~[2; 2])
end
$$\begin{equation} \left[ \begin{array}{c} 1.5 x + 2 y \\ 2 x + 4 y \\ \end{array} \right] = \left[ \begin{array}{c} 2 \\ 2 \\ \end{array} \right] \end{equation}$$
La solución del sistem es:
begin
fsol3=Symbolics.solve_for([f3[1] ~ 2.0,f3[2] ~ 2.0],[x y])
latexify(("[x,y]" ~ [round4(fsol3[1]);round4(fsol3[2])]))
#t1*string(fsol3)"
#println("The solution is [x,y,z]= " * string(fsol3))
end
$$\begin{equation} \left[ \begin{array}{c} x \\ y \\ \end{array} \right] = \left[ \begin{array}{c} 2 \\ -0.5 \\ \end{array} \right] \end{equation}$$
$$\begin{equation} \mathrm{ones}\left( 3, 3 \right) = \left[ \begin{array}{ccc} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \\ \end{array} \right] \end{equation}$$
Otros sistemas y su soluciones
@bind mfac1 Slider(-10.0:10.0,show_value=true,default=1)
$$\begin{equation} M1 = \left[ \begin{array}{ccc} 1 & 3 & 5 \\ 3 & 6 & 10 \\ 1 & 0 & 4.5 \\ \end{array} \right] \end{equation}$$
$$\begin{equation} rango = 3 \end{equation}$$
$$\begin{equation} \left[ \begin{array}{c} x + 3 y + 5 z \\ 2 \left( x + 3 y + 5 z \right) + x \\ x + 4.5 z \\ \end{array} \right] = \left[ \begin{array}{c} 1 \\ 2 \\ 1 \\ \end{array} \right] \end{equation}$$
$$\begin{equation} \left[ \begin{array}{c} x \\ y \\ z \\ \end{array} \right] = \left[ \begin{array}{c} -0 \\ -0.037 \\ 0.2222 \\ \end{array} \right] \end{equation}$$
Operaciones elementales, pivotes, descomposición LU
$$\begin{equation} A = \left[ \begin{array}{ccc} 1 & 3 & 5 \\ 4 & 5 & 6 \\ 7 & 8 & 11 \\ \end{array} \right] \end{equation}$$
begin
#A+[0 0 0;2 -1 0;1 0 -1]*A
#A+[0 0 0;0 0 0;1 0 -1]*A
P1=[1 0 0;4 -1 0;0 0 1]
P2=[1 0 0; 0 1 0;7 0 -1]
P3=[1 0 0; 0 1 0;0 13//7 -1]
PA=P1*A
PPA=P2*PA
PPPA=P3*PPA
latexify("P1" ~ [1 0 0;4 -1 0;0 0 1]),
latexify("P1A" ~ PA),
latexify("P2" ~ [1 0 0; 0 1 0;7 0 -1]),
latexify("P2P1A" ~ PPA),
latexify("P3" ~ [1 0 0; 0 1 0;0 13//7 -1]),
latexify("P1P2P3A" ~ PPPA)
end
#latexify(rank([1 3 5;2 6 10;1 2 0]))
(L"\begin{equation}
P1 = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
4 & -1 & 0 \\
0 & 0 & 1 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P1A = \left[
\begin{array}{ccc}
1 & 3 & 5 \\
0 & 7 & 14 \\
7 & 8 & 11 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P2 = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
7 & 0 & -1 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P2P1A = \left[
\begin{array}{ccc}
1 & 3 & 5 \\
0 & 7 & 14 \\
0 & 13 & 24 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P3 = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & \frac{13}{7} & -1 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P1P2P3A = \left[
\begin{array}{ccc}
1 & 3 & 5 \\
0 & 7 & 14 \\
0 & 0 & 2 \\
\end{array}
\right]
\end{equation}
")
@bind indp Slider(1:3,show_value=true,default=1)
begin
PS=("P"*string(indp));
PSE=eval(Meta.parse(PS))
PSEI=inv(PSE)
latexify(PS ~ PSE),
if indp < 3
latexify(PS*"^-1" ~ Int.(PSEI))
else
latexify(PS*"^-1" ~ PSEI)
end
#latexify(PS),
#latexify(inv(PS))
end
(L"\begin{equation}
P1 = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
4 & -1 & 0 \\
0 & 0 & 1 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P1^{-1} = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
4 & -1 & 0 \\
0 & 0 & 1 \\
\end{array}
\right]
\end{equation}
")
latexify("A" ~ A),
latexify("SP" ~ [1 0 0;4 -1 -1;0 0 1]),
latexify("SPA" ~ [1 0 0;4 -1 -1;0 0 1]*A),
latexify("4row1A-row2A-row3A" ~ [4*A[1,:]-A[:2,:]-A[3,:]]')
(L"\begin{equation}
A = \left[
\begin{array}{ccc}
1 & 3 & 5 \\
4 & 5 & 6 \\
7 & 8 & 11 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
SP = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
4 & -1 & -1 \\
0 & 0 & 1 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
SPA = \left[
\begin{array}{ccc}
1 & 3 & 5 \\
-7 & -1 & 3 \\
7 & 8 & 11 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
4 row1A - row2A - row3A = \left[
\begin{array}{c}
\left[
\begin{array}{ccc}
-7 & -1 & 3 \\
\end{array}
\right] \\
\end{array}
\right]
\end{equation}
")
inv([1 0 0;4 -1 -1;0 0 1])
3×3 Matrix{Float64}:
1.0 0.0 -0.0
4.0 -1.0 -1.0
0.0 0.0 1.0
Descompoisición LU, A LU?
(L"\begin{equation}
L = \left[
\begin{array}{ccc}
1 & 0 & 0 \\
0.1429 & 1 & 0 \\
0.5714 & 0.2308 & 1 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
U = \left[
\begin{array}{ccc}
7 & 8 & 11 \\
0 & 1.8571 & 3.4286 \\
0 & 0 & -1.0769 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
LU = \left[
\begin{array}{ccc}
7 & 8 & 11 \\
1 & 3 & 5 \\
4 & 5 & 6 \\
\end{array}
\right]
\end{equation}
")
Hay una permutación de renglones de por medio en el proceso!
$$PA = LU$$
(L"\begin{equation}
A = \left[
\begin{array}{ccc}
1 & 3 & 5 \\
4 & 5 & 6 \\
7 & 8 & 11 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
P = \left[
\begin{array}{ccc}
0 & 0 & 1 \\
1 & 0 & 0 \\
0 & 1 & 0 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
PA = \left[
\begin{array}{ccc}
7 & 8 & 11 \\
1 & 3 & 5 \\
4 & 5 & 6 \\
\end{array}
\right]
\end{equation}
")
$$\begin{equation} P^{-1} LU = \left[ \begin{array}{ccc} 1 & 3 & 5 \\ 4 & 5 & 6 \\ 7 & 8 & 11 \\ \end{array} \right] \end{equation}$$
permuta renglones 1-> 2, 2 -> 3, 3 -> 1
$$\begin{equation} P \left[ \begin{array}{c} 0 \\ 0 \\ 1 \\ \end{array} \right] = \left[ \begin{array}{c} 1 \\ 0 \\ 0 \\ \end{array} \right] \end{equation}$$
$$\begin{equation} P \left[ \begin{array}{c} 1 \\ 0 \\ 0 \\ \end{array} \right] = \left[ \begin{array}{c} 0 \\ 1 \\ 0 \\ \end{array} \right] \end{equation}$$
latexify(("P"~ FLU.P))
$$\begin{equation} P = \left[ \begin{array}{ccc} 0 & 0 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \\ \end{array} \right] \end{equation}$$
$$\begin{equation} U = L^{-1} PA = \left[ \begin{array}{ccc} 7 & 8 & 11 \\ 0 & 1.8571 & 3.4286 \\ 0 & 0 & -1.0769 \\ \end{array} \right] \end{equation}$$
$$PA = LU, PAx = Pb, LUx = Pb, Ux = c, Lc = Pb$$
Resuelve para c (sistema Lc=Pb ) y luego resuelve Ux=c para obtener x
$$\begin{equation} c = L^{-1} P \left[ \begin{array}{c} 2 \\ 0 \\ 0 \\ \end{array} \right] = \left[ \begin{array}{c} 0 \\ 2 \\ -0.4615 \\ \end{array} \right] \end{equation}$$
3-element Vector{Float64}:
0.0
2.0
-0.4615384615384618
(L"$U \cdot \left[
\begin{array}{c}
x \\
y \\
z \\
\end{array}
\right] = c$", L"\begin{equation}
\left[
\begin{array}{c}
7 x + 8 y + 11 z \\
1.8571 y + 3.4286 z \\
- 1.0769 z \\
\end{array}
\right] = \left[
\begin{array}{c}
0 \\
2 \\
-0.462 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
\left[
\begin{array}{c}
x \\
y \\
z \\
\end{array}
\right] = \left[
\begin{array}{c}
-1 \\
0.2857 \\
0.4286 \\
\end{array}
\right]
\end{equation}
")
La solución 'back slash' [x;y;z]=A\b
begin
Definimos la función
function lf0(A,b)
return A\b
end
[x y, z]=lf0(A,[b1;b2;b3])
end
#Defined above remember!
#using Latexify
#struct Ket{T}
# x::T
#end
#@latexrecipe function f(x::Ket)
#return Expr(:latexifymerge, "", x.x, "")
#end
begin
lf0(A,b)=A\b
xsol0=lf0(A,[2.0;0.0;0.0])
#strAb=(A\b)
b3=[2.0;0.0;0.0]
#latexify(:(x*$(Ket(A[:,1])) + y*$(Ket(A[:,2]))+z*$(Ket(A[:,3])))),
latexify(:($(Ket(A[:,:]))*[x;y;z] = [b1;b2;b3])),#*[x;y;z]), #+ y*$(Ket(A[:,2]))+z*$(Ket(A[:,3])))),
latexify("b" ~ b3),
#latexify(strAb)
latexify(("[x,y,z]" ~ round.(xsol0,digits=4)))
end
(L"$\left[
\begin{array}{ccc}
1 & 3 & 5 \\
4 & 5 & 6 \\
7 & 8 & 11 \\
\end{array}
\right] \cdot \left[
\begin{array}{c}
x \\
y \\
z \\
\end{array}
\right] = \left[
\begin{array}{c}
b1 \\
b2 \\
b3 \\
\end{array}
\right]$", L"\begin{equation}
b = \left[
\begin{array}{c}
2 \\
0 \\
0 \\
\end{array}
\right]
\end{equation}
", L"\begin{equation}
\left[
\begin{array}{c}
x \\
y \\
z \\
\end{array}
\right] = \left[
\begin{array}{c}
-1 \\
0.2857 \\
0.4286 \\
\end{array}
\right]
\end{equation}
")
Cambia el lado derecho de la ecuación y resuelve x=A\b
latexify("b" ~ [-2,1.0,2.0] )
$$\begin{equation} b = \left[ \begin{array}{c} -2 \\ 1 \\ 2 \\ \end{array} \right] \end{equation}$$
begin
lf(A,b)=A\b
xsol1=lf(A,[-2.0;1.0;2.0])
latexify(("[x,y,z]" ~ round.(xsol1,digits=3)))
end
$$\begin{equation} \left[ \begin{array}{c} x \\ y \\ z \\ \end{array} \right] = \left[ \begin{array}{c} 1.5 \\ -0.571 \\ -0.357 \\ \end{array} \right] \end{equation}$$
Cuadrados mínimos, caso típico de problema sobre-determinado
@bind inoise Slider(0.0:0.2:5,show_value=true,default=1)
begin
flsq = Figure(size = (500, 500))
axl=Axis(flsq[1, 1], backgroundcolor = "beige")
tt=LinRange(0,10,100);
#tt=0:.1:10.0;
noise=inoise*randn(length(tt));
Att=[tt[:] ones(length(tt),1)];
xv=[1.0,3.5];
xlsq=lf0(Att,Att*xv+noise);
yobs=Att*xv.+noise
yv=Att*xv
ylsq=Att*xlsq
pointsobs = Point2f.(tt, yobs)
pointsv=Point2f.(tt, yv)
lines!(axl,pointsv,linewidth=4.0,color=:black)
scatter!(axl,pointsobs)
lines!(axl,tt,ylsq,linewidth=4.0,color=:red)
#scatter(tt,Att*xv+noise)#plot(tt,Att*xlsq,color=:red)
#lines(tt,yv)
#xlsq=lf0(Att,Att*xv+noise);
#size(noise)
#size(Att*xv)
#limits!(axl,0.0,10.0,0.0,16.0)
flsq
end